﻿/**
*    @file:    qpagehelper.cpp
*    @brief:
*    @author:  sanshi522@163.com
*    @date:    2025-01-06  10:17
*/
#include "qpagehelper.h"
#include "ui_qpagehelper.h"

QPageHelper::QPageHelper(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::QPageHelper)
{
    ui->setupUi(this);
    itemTotal = 0;
    currentPage = 1;
    beginPage = 1;
    pageCount = ui->pageCount->currentText().toInt();
    ui->pageCount->setValidator(new QIntValidator(1, 10000, this));
    resetContol();
}

QPageHelper::~QPageHelper()
{
    delete ui;
}

void QPageHelper::resetContol()
{
    //总条目显示
    ui->itemTotal->setText(QString("共%1条").arg(itemTotal));
    //计算总共页码数
    pageTotal = itemTotal / ui->pageCount->currentText().toInt();
    int pagea = itemTotal % ui->pageCount->currentText().toInt();

    if (pagea > 0)
    {
        pageTotal++;
    }

    lastPage = currentPage - 1;
    nextPage = currentPage + 1;
    endPage = pageTotal;

    ui->gotoNum->setMinimum(beginPage);
    ui->gotoNum->setMaximum(endPage);
    ui->gotoNum->setValue(currentPage);

    ui->pageCount->setEditText(QString::number(pageCount));

    ui->beginPage->setText(QString("首页"));
    ui->lastPage->setText(QString::number(lastPage));
    ui->nowPage->setText(QString::number(currentPage));
    ui->nextPage->setText(QString::number(nextPage));
    ui->endPage->setText(QString("尾页"));

    ui->lastPageBtn->setVisible(pageTotal > 0 && currentPage - beginPage >= 1);
    ui->beginPage->setVisible(pageTotal > 0 && currentPage - beginPage >= 2);
    ui->lastOmit->setVisible(pageTotal > 0 && currentPage - beginPage >= 2);
    ui->lastPage->setVisible(pageTotal > 0 && lastPage >= beginPage);
    ui->nowPage->setVisible(pageTotal > 0);
    ui->nextPage->setVisible(pageTotal > 0 && nextPage <= endPage);
    ui->nextOmit->setVisible(pageTotal > 0 && endPage - currentPage >= 2);
    ui->endPage->setVisible(pageTotal > 0 && endPage - currentPage >= 2);
    ui->nextPageBtn->setVisible(pageTotal > 0 && endPage - currentPage >= 1);

    ui->gotoLabel->setVisible(pageTotal > 0);
    ui->gotoNum->setVisible(pageTotal > 0);
    ui->gotoBtn->setVisible(pageTotal > 0);

    ui->pageCountLabel->setVisible(pageTotal > 0);
    ui->pageCount->setVisible(pageTotal > 0);
}

void QPageHelper::on_beginPage_clicked()
{
    currentPage = beginPage;
    pageChange();
}

void QPageHelper::on_endPage_clicked()
{
    currentPage = endPage;
    pageChange();
}

void QPageHelper::on_nextPageBtn_clicked()
{
    currentPage += 1;
    pageChange();
}

void QPageHelper::on_lastPageBtn_clicked()
{
    currentPage--;
    pageChange();
}

void QPageHelper::on_gotoBtn_clicked()
{
    currentPage = ui->gotoNum->text().toInt();
    pageChange();
}

void QPageHelper::on_lastPage_clicked()
{
    on_lastPageBtn_clicked();
}

void QPageHelper::on_nextPage_clicked()
{
    on_nextPageBtn_clicked();
}

void QPageHelper::on_pageCount_activated(const QString &arg1)
{
    pageCount = ui->pageCount->currentText().toInt();
    currentPage = beginPage;
    pageChange();
}

int QPageHelper::getPageCount() const
{
    return pageCount;
}

void QPageHelper::setPageCount(int value)
{
    pageCount = value;
    resetContol();
}

qlonglong QPageHelper::getItemTotal() const
{
    return itemTotal;
}

qlonglong QPageHelper::getCurrentPage() const
{
    return currentPage;
}

void QPageHelper::setCurrentPage(const qlonglong &value)
{
    currentPage = value;
    resetContol();
}

void QPageHelper::setItemTotal(const qlonglong &value)
{
    itemTotal = value;
    resetContol();
}

qlonglong QPageHelper::getPageTotal() const
{
    return pageTotal;
}

void QPageHelper::pageChange()
{
    qDebug() << __FUNCTION__;
    resetContol();
    emit signal_goto(currentPage, pageCount);
}
